/**
 * Created by hcadmin on 16/4/9.
 */
/**
 * Created by hcadmin on 16/4/6.
 */
define([
    "jquery",
    "IO",
    "bootbox"
], function ($, IO, bootbox) {

    $(function () {

        var year_1=1;
        var year_2=2;
        var year_3=3;

        var activity_mid=localStorage.getItem("activity_mid");
        gettable(year_1,activity_mid,'F3','华东');
        //查询
        $("#searchid1").bind("click", function () {
            var production_name= $("#selectgroup1 option:selected").attr("data-id");
            var market= $("#selectgroup1 option:selected").attr("data-market");
            if(production_name=="undefined"){
                production_name="";
            }
            if(market=="undefined"){
                market="";
            }
            gettable(year_1,activity_mid,production_name,market);
        });
        $("#searchid2").bind("click", function () {
            var production_name= $("#selectgroup2 option:selected").attr("data-id");
            var market= $("#selectgroup2 option:selected").attr("data-market");
            if(production_name=="undefined"){
                production_name="";
            }
            if(market=="undefined"){
                market="";
            }
            gettable(year_2,activity_mid,production_name,market);
        });
        $("#searchid3").bind("click", function () {
            var production_name= $("#selectgroup3 option:selected").attr("data-id");
            var market= $("#selectgroup3 option:selected").attr("data-market");
            gettable(year_3,activity_mid,production_name,market);
        });

        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            // 获取已激活的标签页的名称
            var activeTab = $(e.target).text();
            // 获取前一个激活的标签页的名称
            if(activeTab=="第一年度"){
                gettable(1,activity_mid,'F3','华东');
            }
            else if(activeTab=="第二年度"){
                gettable(2,activity_mid,"F3","华北");
            }
            else {
                gettable(3,activity_mid,"F3","欧洲");
            }

        });

    });
    function gettable(year_id,activity_mid,production_name,market){
        var parmas={};
        parmas.year_id=year_id;
        parmas.activity_mid=activity_mid;
        parmas.production_name=production_name;
        parmas.market=market;
        var i=false;
        IO.post('/shapan/api/selectMenuOne',parmas,function(d){
            if(d.code==0 && d.data.length>0){
                console.log(d.data);
                IO.post('/shapan/api/selectMenuTwo',parmas,function(p){
                    var html='';
                    for(var i=1;i<= d.data.length;i++){
                        html+='<tr>';
                        if(i==1){
                            html+='<td rowspan="6">'+d.data[i-1].production_name+"-"+d.data[i-1].market+'</td>';
                        }
                        html+='<td>'+ d.data[i-1].group_name+'</td>';
                        if(d.data[i-1].ispremium==0) {
                            html+='<td>'+"1.2"+'</td>';
                        }
                        else{
                            html+='<td>'+"0"+'</td>';
                        }
                        html+='<td>'+d.data[i-1].paiming1+'</td>'+
                                '<td>'+i+'</td>';
                        if(i==1){
                            html+='<td rowspan="6">'+p.data[i-1].production_name+"-"+p.data[i-1].market+'</td>';
                        }
                        html+='<td>'+ p.data[i-1].group_name+'</td>';
                        if(p.data[i-1].ispremium==0) {
                            html+='<td>'+"1.2"+'</td>';
                        }
                        else
                        {
                            html+='<td>'+"0"+'</td>';
                        }
                             html+= '<td>'+p.data[i-1].paiming2+'</td>'+
                                '<td>'+i+'</td>'
                    }
                    $("#tbody"+year_id).empty().append(html);
                })
            }
        })
    }
});